import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:hygf/util/string_extension.dart';
import '../../../config/StyleConfig.dart';
import '../../../res/HDColors.dart';
import '../../../view/common_widget.dart';
import '../../../view/empty_data_widget.dart';
import 'logic.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';

class ScanPage extends StatelessWidget {
  final logic = Get.put(ScanPageLogic());
  final state = Get.find<ScanPageLogic>().state;

  @override
  Widget build(BuildContext context) {
    return WillPopScope(
        onWillPop: () async {
          logic.closePage();
          return false;
        },
        child: Scaffold(
          appBar: AppBar(
              title: const Text("扫码页面"),
              elevation: 0,
              backgroundColor: HDColors.green,
              actions: [
                GetBuilder<ScanPageLogic>(builder: (logic) {
                  return Visibility(
                      visible: true,
                      child: TextButton(
                          onPressed: () {
                            logic.scan();
                          },
                          child: const Text("点击扫码",
                              style: TextStyle(
                                  color: HDColors.white,
                                  fontSize: StyleConfig.fontContentSize14))));
                }),
              ],
              leading: CommonWidget.getAppBarLeading(willPopCallback: () async {
                logic.closePage();
                return false;
              })),
          body: GetBuilder<ScanPageLogic>(builder: (logic) {
            return buildList();
          }),
        ));
  }

  /// 数据列表
  buildList() {
    return GetBuilder<ScanPageLogic>(builder: (logic) {
      return EasyRefresh(
          enableControlFinishRefresh: false,
          enableControlFinishLoad: false,
          controller: state.controller,
          emptyWidget: state.dataList.isEmpty ? const EmptyDataWidget() : null,
          onRefresh: () async {
            state.noMore = true;
            state.controller.finishRefresh();
          },
          onLoad: state.noMore ? null : () async {
            state.noMore = true;
            state.controller.finishRefresh();
          },
          child: ListView.separated(
            shrinkWrap: true,
            itemCount: state.dataList.length,
            itemBuilder: (BuildContext context, int index) {
              return buildProfileItem(index, state.dataList);
            },
            separatorBuilder: (BuildContext context, int index) {
              return Container(height: 10);
            },
          ));
    });
  }

  buildProfileItem(index, List<dynamic> dataList) {
    String item = dataList[index];
    double height = 40;
    return Card(
      margin: EdgeInsets.fromLTRB(20, 5, 20, 0),
      color: HDColors.white,
      shadowColor: Colors.amber,
      elevation: 1,
      shape: RoundedRectangleBorder(
          side: BorderSide(width: 0.3, color: HDColors.divider),
          borderRadius: BorderRadius.all(new Radius.circular(5))),
      child: Container(
          width: double.infinity,
          height: height,
          padding: EdgeInsets.all(10),
          child: Stack(children: [
            Row(
              children: [
                Text("${index+1}: ",
                    style: const TextStyle(
                        fontSize: StyleConfig.fontContentSize16,
                        color: HDColors.black)),
                const SizedBox(width: 10),
                Expanded(
                    child: Text(item ?? "",
                        maxLines: 1,
                        overflow: TextOverflow.ellipsis,
                        style: const TextStyle(
                            fontSize: StyleConfig.fontContentSize16,
                            color: HDColors.black))),
                CommonWidget.buildIconWidget(
                    showIcon: false, nodestatue: "")
              ],
            ),
            Visibility(
                visible: !state.isReadOnly,
                  child: Positioned(
                    right: 5,
                    top: 0,
                    child: InkWell(
                      onTap: () {
                        logic.removeItem(index);
                      },
                      child: Icon(Icons.delete, color: Colors.red,size: 20),
                    )
                  ),
                )
          ])),
    );
  }
}
